//app.js
App({
  onLaunch: function () {
    console.log("onLaunch");
    var userinfo = wx.getStorageSync("userInfo");
    var id = wx.getStorageSync("id");
    var shop_id = wx.getStorageSync("shop_id");
    if (this.isnull(userinfo)) {
      this.init();
    } else {
    }
    // if (this.isnull(id) || this.isnull(shop_id)) {
    //   wx.reLaunch({
    //     url: 'pages/index/index'
    //   })
    // } else {
    //   wx.reLaunch({
    //     url: 'pages/home/home'
    //   })
    // }
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
  },
  init() {
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              wx.setStorageSync("userInfo", res.userInfo);
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        } else {
          console.log("未授权");
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              this.init();
            }
          })
        }
      }
    })
  },
  isnull(val) {
    var res = val == null || val == undefined || val.length == 0;
    return res;
  },
  globalData: {
    version: "1.0",
    sysinfo: wx.getSystemInfoSync(),
    gymlogo: "/assets/image/minelogo.png",
    id: wx.getStorageSync("id"),
    face1: wx.getStorageSync("face1"),
    face1_status: wx.getStorageSync("face1_status"),
    mobile: wx.getStorageSync("mobile"),
    userInfo: wx.getStorageSync("userInfo"),
    shop_id: wx.getStorageSync("shop_id"),
    name: wx.getStorageSync("name"),
    shop_name: wx.getStorageSync("shop_name"),
    hasUserInfo: false,
    isNavigating: false
  }
})